# -*- coding: UTF-8 -*-
import scrapy
import messAround.util.help as util


# Acfun热门排行榜
# https://blog.csdn.net/phoenix/web/blog/hotRank?page=2&pageSize=20
# scrapy crawl acfun_rank
class AcfunTopSpider(scrapy.Spider):
    source = 0

    name = 'csdn'

    allowed_domains = ['www.acfun.cn']

    start_urls = ['https://www.acfun.cn']

    headers = {
        'User-Agent': util.agent,
        ':authority': 'www.acfun.cn',
        'referer': 'https://www.acfun.cn/rank/list',
        'sec-fetch-dest': 'empty',
        'Sec-Fetch-mode': 'cors',
        'Sec-Fetch-site': 'same-origin',
        'x-requested-with': 'XMLHttpRequest',
    }

    custom_settings = {
        'ITEM_PIPELINES': {
            'messAround.pipeline.acfun.AcFunRankPipeline': 300
        }
    }

    def parse(self, response):
        res_url = 'https://www.acfun.cn/rest/pc-direct/page/queryNavigators'
        yield scrapy.Request(url=res_url, headers=util.default_headers, callback=self.parse_url)

    def parse_url(self, response):
        res = util.make_json(response.text)
        if res['code'] == 200:
            url_list = [
                {
                    "link": 'https://www.acfun.cn/rest/pc-direct/rank/channel?channelId=&subChannelId=&rankLimit=30&rankPeriod=DAY',
                    "nav_channel_id": "",
                    "nav_channel_name": "全站综合",
                },
            ]

            for val in res['data']:
                url = 'https://www.acfun.cn/rest/pc-direct/rank/channel?channelId=' + str(
                    val['cid']) + '&subChannelId=&rankLimit=30&rankPeriod=DAY'

                url_list.append({
                    "link": url,
                    "nav_channel_id": val['cid'],
                    "nav_channel_name": val['navName'],
                })

            for item in url_list:
                yield scrapy.Request(url=item['link'],
                                     meta=item,
                                     headers=util.default_headers,
                                     callback=self.parse_channel_list, dont_filter=True)
                pass

    def parse_channel_list(self, response):
        result = util.make_json(response.text)
        if result['result'] == 0:
            for key, val in enumerate(result['rankList']):
                yield {
                    # nav_channel_id
                    'nav_channel_id': response.meta['nav_channel_id'],
                    # nav_channel_name
                    'nav_channel_name': response.meta['nav_channel_name'],
                    'source': self.source,
                    'no': key + 1,
                    # 标题
                    'title': val['title'],
                    # 连接
                    'link': 'https://www.acfun.cn/v/' + val['dougaId'],
                    # 简介
                    'info': val['description'],
                    # 封面
                    'cover': val['coverUrl'],
                    # 所属频道ID
                    'channel_id': val['channelId'],
                    # 所属频道名称
                    'channel_name': val['channelName'],
                    # 浏览量
                    'play_count': val['viewCount'],
                    # 浏览量展示文本
                    'play_count_text': val['viewCountShow'],
                    # 弹幕数量
                    'danmaku_count': val['danmakuCount'],
                    # 弹幕数量文本
                    'danmaku_count_text': val['danmakuCountShow'],
                    # 视频ID
                    'video_id': val['dougaId'],
                    # 视频时常
                    'video_duration': val['durationMillis'],
                    # 移动端播放地址
                    'mobile_link': val['shareUrl'],
                    # 分享数量
                    'share_count': val['shareCount'],
                    # 分享数量文本
                    'share_count_text': val['shareCountShow'],
                    # 点赞数量
                    'like_count': val['likeCount'],
                    # 点赞数量文本
                    'like_count_text': val['likeCountShow'],
                    # up主相关
                    "author_id": val['userId'],
                    "author_name": val['userName'],
                    "author_signature": val['userSignature'],
                    "author_fans_count": val['user']['fanCountValue'],
                    "author_fans_count_text": val['fansCount'],
                    "author_avatar": val['userImg'],
                }
            pass
